#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pthread.h>


#define SOFTAP_PORT             8082
#define SOFTAP_IP               "192.168.1.121"

#define WLAN_SSID_MAX_LEN       32
#define WLAN_PASSWD_MAX_LEN     63

#define TEST_SSID   "hklt001"
#define TEST_PASSWD "hklt001"

int cfd = -1;

typedef struct{
    unsigned char ssid[WLAN_SSID_MAX_LEN];
    unsigned char pwd[WLAN_PASSWD_MAX_LEN];
    unsigned char ssid_len;
    unsigned char pwd_len;
    unsigned char random;
}WIFI_INFO;

int main(void)
{
    int ret = -1;
    int sockfd = -1;
    char *data = (char *)malloc(1024);
 
    sockfd = socket(PF_INET, SOCK_STREAM, 0);
    if(sockfd == -1)
    {
        printf("socker fail\n");
        return -1;
    }
    
    struct sockaddr_in saddr;
    saddr.sin_family = AF_INET; //制定ip地址格式
    saddr.sin_port = htons(SOFTAP_PORT);
    saddr.sin_addr.s_addr = inet_addr(SOFTAP_IP);

    ret = bind(sockfd, (struct sockaddr *)&saddr, sizeof(saddr));
    if(ret == -1)
    {
        printf("bind fail\n");
        return -1;
    }

    ret = listen(sockfd, 3);
    if(ret == -1)
    {
        printf("listen fail.\n");
        return -1;
    }

    struct sockaddr_in clnaddr = {0};
    int clnaddr_size = sizeof(clnaddr);

    cfd = accept(sockfd, (struct sockaddr *)&clnaddr, (socklen_t *)&clnaddr_size);
    if(cfd == -1)
    {
        printf("accept fail.'n");
        return -1;
    }
    printf("clint_port = %d, clint_ip = %s\n", ntohs(clnaddr.sin_port), inet_ntoa(clnaddr.sin_addr));

    while(1)
    {
        memset(data, 0, 1024);
        ret = recv(cfd, data, 1024, 0);
        if(ret > 0)
        {
            printf("server recv %s\n", data);
            send(cfd, "recv ok", 7, 0);
        }



       sleep(1);
    }




}